﻿using CPFrameWork.Global;
using CPFrameWork.Utility.DbOper;
using System;
using System.Collections.Generic;
using System.Text;

namespace BusinessManage.Common
{
    public static class OrderLog
    {

        public static void OrderOperateLog(OrderOperateLog orderLog) {
            string TaskId = orderLog.taskid;
            string OrderId = orderLog.orderid;
            string UserId = CPExpressionHelper.Instance.RunCompile("${CPUser.UserId()}"); 
            string UserName = CPExpressionHelper.Instance.RunCompile("${CPUser.UserName()}");
            string timenow = DateTime.Now.ToString();
            string operatename = orderLog.OperateName;
            string OperateDescribe = string.Empty;
            if(string.IsNullOrEmpty( TaskId)) OperateDescribe= UserName + "在" + timenow + "对订单" + OrderId + "进行了"+ operatename + "操作";
            else
                OperateDescribe = UserName + "在" + timenow + "对任务" + TaskId + "、订单" + OrderId + "进行了"+ operatename + "操作";
            string LinkType = orderLog.LinkType;
            string LinkName = orderLog.LinkName;
            string DbIns = orderLog.DbIns == "" ? "CPYKWL" : orderLog.DbIns;
            //插入日志信息表            
            string strSql = "INSERT INTO Sys_OperateOrderLog ";
            strSql += " (OrderId,TaskId,OperateType,OperateName,OperUrl,ParamValue,BeforeContent,AfterContent,OperateDescribe,OperateUserId,OperateUserName,OperateTime) ";
            strSql += " values('" + OrderId + "'";
            strSql += ",'" + TaskId + "'";
            strSql += ",'"+ LinkType + "'";
            strSql += ",'"+ LinkType + "'";
            strSql += ",''";
            strSql += ",''";    //修改
            strSql += ",''";
            strSql += ",''";
            strSql += ",'" + OperateDescribe + "'";
            strSql += ",'" + UserId + "'";
            strSql += ",'" + UserName + "'";
            strSql += ",'" + timenow + "'";
            strSql += ");";        
            StringBuilder sb = new StringBuilder();
            sb.Append(strSql);
            DbHelper dbHelper = new DbHelper(DbIns, CPAppContext.CurDbType());
            if (sb.Length > 0)
            {
                dbHelper.ExecuteNonQuery(sb.ToString());
            }
        }
    }


    public class OrderOperateLog {
        public string LinkType { get; set; }//环节类型
        public string LinkName { get; set; }//环节名称
        public string orderid { get; set; }//订单编码
        public string taskid { get; set; }//任务编码
        public string DbIns { get; set; }//操作数据库实例
        public string OperateName { get; set; }//操作名称
    }
}
